home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15565 < prev    next >
Encoding:
Text File  |  1996-08-05  |  10.0 KB  |  427 lines

  1. Path: nuscc.nus.sg!wongsiaw
  2. From: wongsiaw@iscs.nus.sg (Virus)
  3. Newsgroups: comp.lang.c++
  4. Subject: [HELP!!!] Segmentation Fault!
  5. Date: 6 Apr 1996 01:15:22 GMT
  6. Organization: ...I am a wuggies...
  7. Message-ID: <4k4gja$mm@nuscc.nus.sg>
  8. NNTP-Posting-Host: wongsiaw@ibmunx.iscs.nus.sg
  9. X-Newsreader: TIN [version 1.2 PL2]
  10.  
  11. Help !!!
  12. [This is going to be a very long mail, I sincerely hope that you can
  13. read thro it to help a novice C++ programmer.]
  14.  
  15. I keep getting segmentation fault in my program, and I have tried using the
  16. debugger (gdb in unix), but I cannot understand what the debugger is trying
  17. to say..
  18.  
  19. [my story]
  20. I have 2 files, that I am suppose to read in, one is space-separated and
  21. the other is comma-separated. In each of the files, there are student
  22. records, each terminated by a "end".
  23.  
  24. This is the space-separated file, sfile.data .
  25.    
  26. space-separated
  27.  
  28. Undergrad "Khoo Kay Chong" 946788H13 
  29. IC252 90 IC251 95 IC205 85 end
  30.  
  31. Graduate "Lee Su Shi"   901234N13 Master TA
  32. IC401 80 IC501  85 end
  33.  
  34. Graduate "James Bond"     880007N13 PhD RA
  35. IC405 85 IC504 90 IC512 95 end
  36.  
  37. Exchange "Tom Hanks" 905678N13 "Stanford University"
  38. IC101 80 end
  39.  
  40.  
  41. And the comma-separated file looks the same except that it uses the
  42. comma to separate the fields of the record.
  43.  
  44. I am to write 3 classes, StudentFile, SpaceStudentFile and CommaStudentFile.
  45. I have already done those, but I ran into segmentation fault when I
  46. try to run my compiled program.
  47.  
  48. here are the files..
  49.  
  50. If you think you need to see Student.h, Student.c String.h, String.c,
  51. ExchStudent.h ExchStudent.c, GradStudent.h, GradStudent.c, pls e-mail
  52. me and let me know.
  53.  
  54. I really appreciate your help, as this is only part of my program.
  55. I still have other classes to write that access this class to hand in
  56. my final program. (but currently I am stuck here !!!)
  57.  
  58. Thanx!!!
  59.  
  60. regards,
  61. Diana
  62. ps : I hope that all the comments is sufficient for you to understand my
  63.      codes.
  64.  
  65. -------------------------------------------------------------------------
  66.  
  67. //StudentFile.h
  68.  
  69. // Need to put "virtual" in front of the object name  and 
  70. // make it = 0 if it is a deferred obj
  71.  
  72. #ifndef StudentFile_h
  73. #define StudentFile_h
  74.  
  75. #include "Student.h"
  76.  
  77.  
  78. // Constants that needed to be used in the program
  79. #define UNDERGRAD    "Undergrad"
  80. #define GRADUATE    "Graduate"
  81. #define EXCHANGE    "Exchange"
  82. #define PHD        "PhD"
  83. #define MASTER        "Master"
  84. #define SCHOLARSHIP    "Scholarship"
  85. #define TEACH        "Teach"
  86. #define RESEARCH    "Research"
  87. #define NONE        "None"
  88. #define END        "end"
  89.  
  90. class StudentFile
  91. {
  92. public :
  93.     // Public deferred method
  94.     // For opening a file for reading
  95.     // Opens the file by the name filename for reading
  96.     // if file exists, can be opened and the opened file has the
  97.     // correct file-type, then it returns non-0 (true)
  98.     // Otherwise, returns 0 (false)
  99.     virtual int open (const char *filename) = 0;
  100.  
  101.     // Public deferred method
  102.     // For closing a file
  103.     virtual void close () = 0;
  104.  
  105.     // Public polymorphic (non-deferred) method
  106.     // For reading a student's record
  107.     // Reads the next student's record, stores it in a dynamically
  108.     // created instance, and returns a pointer to the instance
  109.     // The dynamically created instance should belong to either
  110.     // Student, GradStudent, ExchStudent class depending on the
  111.     // student-type
  112.     // Returns NULL if there is no more record in the file
  113.     Student *readNextRecord ();
  114.  
  115. protected :
  116.     // Protected deferred method
  117.     // For reading a field from the file
  118.     // Reads the next field from the file and returns a pointer
  119.     // to the field
  120.     // This method is used to support readNextRecord
  121.     virtual char *readNextField ()  = 0;
  122.  
  123.  
  124.     FILE *fd;    
  125. };
  126.  
  127. #endif
  128.  
  129.  
  130. -----------------------------------------------------------------------
  131.  
  132. // I think the problem is here !!!
  133. // StudentFile.c
  134.  
  135. #include <string.h>
  136. #include "StudentFile.h"
  137. #include "GradStudent.h"
  138. #include "ExchStudent.h"
  139.  
  140.  
  141. // Public polymorphic (non-deferred) method
  142. // For reading a student's record
  143. // Reads the next student's record, stores it in a dynamically
  144. // created instance, and returns a pointer to the instance
  145. // The dynamically created instance should belong to either
  146. // Student, GradStudent, ExchStudent class depending on the
  147. // student-type
  148. // Returns NULL if there is no more record in the file
  149.  
  150. Student* StudentFile::readNextRecord ()
  151. {
  152.   Student *Stud;
  153.   char *field1, *field2, *field3, *field4, *field5;
  154.  
  155.   // field1 - field5 are used for reading in of diff fields in a record
  156.   // field1 read in from the record, this field now states the student-type
  157.  
  158.  
  159. //  strcpy(field1, readNextField());
  160.   field1 = (readNextField());
  161.  
  162.     printf("%s\n",field1); //debug stmt
  163.  
  164.  
  165.   if (field1 == NULL) return NULL;
  166.   // nothing to read in
  167.  
  168.     printf("%s\n",field1);//debug stmt
  169.  
  170.   field2 = (readNextField());
  171.   // next field is the StudName, so get the Student name in a record
  172.  
  173.     printf("%s\n",field2);//debug stmt
  174.  
  175.     printf("%s %s \n", field1, field2);  
  176.   field3 = strdup(readNextField());
  177.   // get the Student number in a record
  178.  
  179.     printf("%s\n",field3);//debug stmt
  180.  
  181.   // field1 has already read in the Student type, ie Graduate, Exchange, etc
  182.   // so now need to check what is being read in
  183.  
  184.     printf("%s\n", field1);//debug stmt
  185.   
  186.     // BIG problem !!! field1 now actually has the content of
  187.     // field3 !!! in fact all the field1, field2 and field3 has the
  188.     // same content !!! WHY????
  189.  
  190.   // Check for Undergraduates
  191.   if (!strcmp(field1, "Undergrad")) {
  192.     printf("Create Undergrad here!\n");
  193.     Stud = new Student(field2, field3);
  194.   // The student type read in is Undergrade, an instance of Student class 
  195.   // will be created, and field2 = StudName, field3 = StudNo
  196.  
  197.     printf("Undergrad created!\n");
  198.   }
  199.  
  200.   // Check for Graduates
  201.   else if (!strcmp(field1, GRADUATE))
  202.   {
  203.     int Stud_lvl, Assist_type;
  204.         
  205.   // Now check the Gradute level
  206.         if (!strcmp(field4 = readNextField(), PHD))
  207.             Stud_lvl = GradStudent::PhD;
  208.         else if (!strcmp(field4, MASTER))
  209.             Stud_lvl = GradStudent::Master;
  210.  
  211.   // Now check the assistant-ship, make field4 read the next field
  212.         if (!strcmp(field4 = readNextField(), TEACH))
  213.             Assist_type = GradStudent::TA;
  214.         else if (!strcmp(field4, SCHOLARSHIP))
  215.             Assist_type = GradStudent::Scholarship;
  216.         else if (!strcmp(field4, RESEARCH))
  217.             Assist_type = GradStudent::RA;
  218.         else if (!strcmp(field4, NONE))
  219.             Assist_type = GradStudent::None;
  220.  
  221.   // An instance of GradStudent class will be created
  222.         GradStudent *Grad = new GradStudent(field2, field3, Stud_lvl, Assist_type);
  223.   // field2 = StudName, field3 = StudNo,
  224.         Stud = Grad;
  225.   }  
  226.  
  227.   // Check for Exchange Students
  228.   else if (!strcmp(field1, EXCHANGE))
  229.   {
  230.   // An instance of ExchStudent class will be created
  231.         ExchStudent *Exch = new ExchStudent(field2, field3, readNextField());
  232.   // field2 = StudName, field3 = StudNo, readNextField() = parentU
  233.         Stud = Exch;
  234.   }
  235.   
  236.   // Now read course names and grades
  237.   while (!strcmp(field5 = readNextField(), END))
  238.   {
  239.     int Course_Gr;
  240.  
  241.     // read the grade of the course
  242.     sscanf(readNextField(), "%i", &Course_Gr);
  243.     // field5 = course_name
  244.     Stud -> assignGrade(field5, Course_Gr);
  245.   }
  246.  
  247.   return Stud;
  248. } // end of readNextRecord
  249.  
  250. -------------------------------------------------------------------------
  251.  
  252. // SpaceStudentFile.h
  253.  
  254. #ifndef SpaceStudentFile_h
  255. #define SpaceStudentFile_h
  256.  
  257. #include "StudentFile.h"
  258.  
  259. class SpaceStudentFile : public StudentFile
  260. {
  261. public :
  262.     // Open the cfile.data for reading..
  263.     int open (const char *filename);
  264.  
  265.     // Close the file after reading
  266.     void close ();
  267.  
  268. private :
  269.  
  270.     // Read the next field from the file - record
  271.     char *readNextField();
  272.  
  273. };
  274.  
  275. #endif
  276.  
  277.  
  278. // SpaceStudentFile.c
  279.  
  280. #include "SpaceStudentFile.h"
  281.  
  282. // Fields in sfile.data are seperated by one or more spaces
  283.  
  284. int SpaceStudentFile::open (const char *filename)
  285. {
  286.   char buffer[20];
  287.  
  288.   fd = fopen (filename, "r");
  289.   if (fd == NULL) {
  290.     printf("Cannot open file : %s \n", filename);
  291.     return 0;
  292.   }
  293.   fscanf(fd, "%s", buffer);
  294.   return(!strcmp(buffer, "space-separated"));
  295. }
  296.  
  297.  
  298. void SpaceStudentFile::close ()
  299. {
  300.   fclose(fd);
  301. }
  302.  
  303. char* SpaceStudentFile::readNextField()
  304. {
  305.   char str[40];
  306.   int i = 0;
  307.  
  308.   // take away starting whitespaces
  309.   while (str[0] = fgetc(fd)) {
  310.       if (*str == ' ') continue;
  311.     if (*str == '\t') continue;
  312.     if (*str == '\n') continue;
  313.     if (*str == EOF) return NULL;
  314.     break;
  315.   }
  316.  
  317.   // read from " to next "
  318.   if (str[0] == '"') {
  319.     for (i=0; ((str[i] = fgetc(fd)) != '"'); i++)
  320.     ;
  321.     str[i]=0;
  322.     return str;
  323.   }
  324.  
  325.   // else read until next whitespace
  326.   for (i=1; ((str[i] = fgetc(fd)) != ' ')
  327.     && (str[i] != '\n') && (str[i] != '\t'); i++)
  328.   ;
  329.   str[i] = 0;
  330.   return str;
  331.  
  332.  
  333. }
  334.  
  335. ----------------------------------------------------------------------
  336.  
  337. // CommaStudentFile.h
  338.  
  339. #ifndef CommaStudentFile_h
  340. #define CommaStudentFile_h
  341.  
  342. #include "StudentFile.h"
  343.  
  344. class CommaStudentFile : public StudentFile
  345. {
  346. public :
  347.     // Open the cfile.data for reading..
  348.     int open (const char *filename);
  349.  
  350.     // Close the file after reading
  351.     void close ();
  352.  
  353. private :
  354.  
  355.     // Read the next field from the file - record
  356.     char *readNextField();
  357.  
  358. };
  359.  
  360. #endif
  361.  
  362. ----------------------------------------------------------------------
  363.  
  364. // CommaStudentFile.c
  365.  
  366. #include "CommaStudentFile.h"
  367.  
  368. // Fields in cfile.data are seperated by whitespaces and commas
  369.  
  370. int CommaStudentFile::open (const char *filename)
  371. {
  372.   char buffer[20];
  373.  
  374.   fd = fopen(filename, "r");
  375.   if (fd == NULL) {
  376.     printf("Cannot open file :%s\n", filename);
  377.     return 0;
  378.   }
  379.   fscanf(fd, "%s", buffer);
  380.   return (!strcmp(buffer, "comma-separated"));
  381.     
  382. }
  383.  
  384.  
  385. void CommaStudentFile::close()
  386. {
  387.   fclose(fd);
  388. }
  389.  
  390. char* CommaStudentFile::readNextField()
  391. {
  392.   char str[40];
  393.   int i=0;
  394.  
  395.   // take away whitespace, for more comments, see SpaceStudentFile.c
  396.   while (str[0] = fgetc(fd)) {
  397.     if (*str == ' ') continue;
  398.     if (*str == '\n') continue;
  399.     if (*str == '\t') continue;
  400.     if (*str == EOF) return "EOF";
  401.     break;
  402.   }
  403.  
  404.   // read until next comma
  405.   for (i=0; ((str[i]=fgetc(fd)) != ',') && (str[i] != '\n'); i++)
  406.   {    printf("%d",i); }
  407.   ;
  408.   printf("\n");
  409.   str[i] =0;
  410.  
  411.   // take away trailing whitespace
  412.   for (; (str[i-1] == ' '); i--);
  413.   str[i+1] = 0;
  414.  
  415.   return str;
  416.   
  417.  
  418. }
  419.  
  420. ------------------------------------------------------------------------
  421.  
  422.  
  423. --
  424.      -== .DV. ==-            -== Windows Users Group (Singapore) ==-
  425. http://www.iscs.nus.sg/~wongsiaw           http://www.ncb.gov.sg/WUG
  426.                                          --
  427.